package backend.game.engine;

import interfaces.game.IEntity;
import interfaces.rmi.client.IClientCallBack;
import interfaces.rmi.server.IModule;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import backend.game.entities.Entity;

public class Engine extends Thread implements IModule
{

	private Collection<IEntity> entities = null;
	private Collection<IEntity> updateList = null;

	public Engine()
	{
		entities = new Vector<IEntity>();
		updateList = new Vector<IEntity>();

		loadWorld();
		loadEntities();
	}

	public void loadEntity(IEntity entity)
	{
		entities.add(entity);
	}

	private void loadWorld()
	{
		loadEntity(new Entity("dirt", 0, 0));
	}

	private void loadEntities()
	{
	}

	@Override
	public void run()
	{
		while (true)
		{
			// TODO process game
			// TODO add to update list
		}
	}

	@Override
	public void doBefore()
	{
		for (IEntity entity : entities)
		{
			updateList.add(entity);
		}
	}

	@Override
	public void process(IClientCallBack connection) throws RemoteException
	{
		// TODO get entities for player / filter

		connection.pushUpdates(updateList);
	}

	@Override
	public void doAfter()
	{
		updateList.clear();
	}

}
